package cc.telecomdigital.tdstock.trading.widget;

import java.util.ArrayList;
import java.util.List;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public abstract class AbstractWheelTextAdapter implements WheelViewAdapter
{
	public static final int		TEXT_VIEW_ITEM_RESOURCE	= -1;
	protected static final int	NO_RESOURCE			= 0;
	public static final int		DEFAULT_TEXT_COLOR		= 0xFF101010;
	public static final int		LABEL_COLOR			= 0xFF700070;
	public static final int		DEFAULT_TEXT_SIZE		= 24;
	private int				textColor				= DEFAULT_TEXT_COLOR;
	private int				textSize				= DEFAULT_TEXT_SIZE;
	protected Context			context;
	protected LayoutInflater		inflater;
	protected int				itemResourceId;
	protected int				itemTextResourceId;

	protected int				emptyItemResourceId;

	protected AbstractWheelTextAdapter(Context context)
	{
		this(context, TEXT_VIEW_ITEM_RESOURCE);
	}

	protected AbstractWheelTextAdapter(Context context, int itemResource)
	{
		this(context, itemResource, NO_RESOURCE);
	}

	protected AbstractWheelTextAdapter(Context context, int itemResource,
			int itemTextResource)
	{
		this.context = context;
		itemResourceId = itemResource;
		itemTextResourceId = itemTextResource;

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getTextColor()
	{
		return textColor;
	}

	public void setTextColor(int textColor)
	{
		this.textColor = textColor;
	}

	public int getTextSize()
	{
		return textSize;
	}

	public void setTextSize(int textSize)
	{
		this.textSize = textSize;
	}

	public int getItemResource()
	{
		return itemResourceId;
	}

	public void setItemResource(int itemResourceId)
	{
		this.itemResourceId = itemResourceId;
	}

	public int getItemTextResource()
	{
		return itemTextResourceId;
	}

	public void setItemTextResource(int itemTextResourceId)
	{
		this.itemTextResourceId = itemTextResourceId;
	}

	public int getEmptyItemResource()
	{
		return emptyItemResourceId;
	}

	public void setEmptyItemResource(int emptyItemResourceId)
	{
		this.emptyItemResourceId = emptyItemResourceId;
	}

	protected abstract CharSequence getItemText(int index);

	public View getItem(int index, View iConvertView, ViewGroup iParentGroup)
	{
		if (index >= 0 && index < this.getItemsCount())
		{
			if (iConvertView == null)
				iConvertView = getView(itemResourceId, iParentGroup);

			TextView textView = getTextView(iConvertView, itemTextResourceId);
			if (textView != null)
			{
				CharSequence text = getItemText(index);
				if (text == null)
					text = "";

				textView.setText(text);

				if (itemResourceId == TEXT_VIEW_ITEM_RESOURCE)
					configureTextView(textView);
			}
			return iConvertView;
		}
		return null;
	}

	@Override
	public View getEmptyItem(View iConvertView, ViewGroup iParentGroup)
	{
		if (iConvertView == null)
			iConvertView = getView(emptyItemResourceId, iParentGroup);

		if (emptyItemResourceId == TEXT_VIEW_ITEM_RESOURCE
				&& iConvertView instanceof TextView)
			configureTextView((TextView) iConvertView);

		return iConvertView;
	}

	protected void configureTextView(TextView iTextViewObj)
	{
		iTextViewObj.setTextColor(textColor);
		iTextViewObj.setGravity(Gravity.CENTER);
		iTextViewObj.setTextSize(textSize);
		iTextViewObj.setLines(1);
		iTextViewObj.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
	}

	private TextView getTextView(View iViewObj, int iTextResId)
	{
		TextView textViewObj = null;
		try
		{
			if (iTextResId == NO_RESOURCE && iViewObj instanceof TextView)
				textViewObj = (TextView) iViewObj;
			else if (iTextResId != NO_RESOURCE)
				textViewObj = (TextView) iViewObj.findViewById(iTextResId);
		}
		catch (ClassCastException e)
		{
			TDStockLog.e("AbstractWheelAdapter",
					"You must supply a resource ID for a TextView");
			throw new IllegalStateException(
					"AbstractWheelAdapter requires the resource ID to be a TextView",
					e);
		}
		return textViewObj;
	}

	private View getView(int resource, ViewGroup parent)
	{
		switch (resource)
		{
			case NO_RESOURCE:
				return null;
			case TEXT_VIEW_ITEM_RESOURCE:
				return new TextView(context);
			default:
				return inflater.inflate(resource, parent, false);
		}
	}

	// Observers
	private List<DataSetObserver>	datasetObservers;

	@Override
	public void registerDataSetObserver(DataSetObserver observer)
	{
		if (datasetObservers == null)
			datasetObservers = new ArrayList<DataSetObserver>();

		datasetObservers.add(observer);
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer)
	{
		if (datasetObservers != null)
			datasetObservers.remove(observer);
	}

	public void notifyDataChangedEvent()
	{
		if (datasetObservers != null)
		{
			for (DataSetObserver observer : datasetObservers)
				observer.onChanged();
		}
	}

	public void notifyDataInvalidatedEvent()
	{
		if (datasetObservers != null)
		{
			for (DataSetObserver observer : datasetObservers)
				observer.onInvalidated();
		}
	}
}
